package popup;

import com.configurator.FieldData;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;

/**
 *
 * @author Sawior
 */
public class PopupSpaceSettingsController {
    @FXML private ChoiceBox fieldType;
    @FXML private TextField spaceNumber;
    @FXML private ChoiceBox spaceType;
    @FXML private ChoiceBox spaceStatus;
    @FXML private Button saveSpace;

    @FXML
    private void initialize() {
        fieldType.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
           
            @Override
            public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
                if(fieldType.getValue() != null) {
                    if(t1.intValue() == 1 || t1.intValue() == 2) {
                        spaceNumber.setDisable(true);
                        spaceType.setDisable(true);
                        spaceStatus.setDisable(true);
                    }
                    else {
                        spaceNumber.setDisable(false);
                        spaceType.setDisable(false);
                        spaceStatus.setDisable(false);
                    }
                }
            }
        });
    }
    
    public Button getSaveSpace() {
        return saveSpace;
    }
    
    public FieldData getDataFromPopup(int x, int y, Integer level) {
        String fieldTypeValue = fieldType.getValue() == null ? null : fieldType.getValue().toString();
        String spaceTypeValue = spaceType.getValue() == null ? null : spaceType.getValue().toString();
        
        String spaceStatusValue = spaceStatus.getValue() == null ? null : spaceStatus.getValue().toString();
        
        return new FieldData(fieldTypeValue, getSpaceNumber(), spaceTypeValue, spaceStatusValue, x, y, level);
    }
    
    private String convertSpaceTypeFromDataBaseEnum(String spaceType) {
        if (spaceType.equals("Osobowe") || spaceType.equals("Personal")) {
            return "Osobowe";
        }
        if (spaceType.equals("Dostawcze") || spaceType.equals("Van")) {
            return "Dostawcze";
        }
        if (spaceType.equals("Motor") || spaceType.equals("Motorcycle")) {
            return "Motor";
        }
        if (spaceType.equals("Przyczepka") || spaceType.equals("Osobowe")) {
            return "Przyczepka";
        }
        if (spaceType.equals("Specialne") || spaceType.equals("Special")) {
            return "Specialne";
        }
        return "Osobowe";
    }
    
    private String convertSpaceStatusFromDataBaseEnum(String spaceStatus) {
        if (spaceStatus.equals("Wolne") || spaceStatus.equals("Free")) {
            return "Wolne";
        }
        if (spaceStatus.equals("Zajęte") || spaceStatus.equals("Occupied")) {
            return "Zajęte";
        }
        if (spaceStatus.equals("Niedostępne") || spaceStatus.equals("Disabled")) {
            return "Niedostępne";
        }

        return "Wolne";
    }
    
    public void putOldData(FieldData oldData) {
        this.fieldType.setValue(oldData.getFieldType());
        String spaceNumber = "";
        if(oldData.getSpaceNumber() != null) {
            spaceNumber = oldData.getSpaceNumber().toString();
        }
        this.spaceNumber.setText(spaceNumber);
        this.spaceStatus.setValue(convertSpaceStatusFromDataBaseEnum(oldData.getSpaceStatus()));
        this.spaceType.setValue(convertSpaceTypeFromDataBaseEnum(oldData.getSpaceType()));
    }
    
    private Integer getSpaceNumber() {
        Integer spaceNumberAsInt = null;
        try {
            spaceNumberAsInt = Integer.parseInt(this.spaceNumber.getText());
        }
        catch(Exception ex) {
            Logger.getLogger(PopupSpaceSettingsController.class.getName()).log(Level.SEVERE, "[CarPark] Cannot parse space number from space popup setting to integer.");
            return null;
        }
        
        return spaceNumberAsInt > 0 ? spaceNumberAsInt : null;
    }

}
